package com.dwusoft.banks.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import utils.Constants;
import utils.HttpUtils;
import utils.SM2Utils;

@Controller
@RequestMapping("/")
public class BackNetTradeHomeController {
	
	private final static Logger logger = LoggerFactory.getLogger(BackNetTradeHomeController.class);
	
//	@RequestMapping("/backNetTradeHome")
	@RequestMapping(value = "{action}",method = RequestMethod.GET)
	public void backNetTradeHome(HttpServletRequest request, HttpServletResponse response){
		//验密解密
		Map<String, Object> reqMap = HttpUtils.getRequestParameters(request);
		String context = (String)reqMap.get(Constants.PARAM_CONTEXT);
		logger.info("接收的密文是：  " + context);
//		String custsm2Path = Constants.PLAT_SM2_PATH;
//		String custPass = Constants.PLAT_SM2_PWD;
		try {
			System.out.println(context);
			context = context.replaceAll(" ", "+");
			System.out.println("=======================================================");
			//暂时写死3001
//			context = SM2Utils.decrypt(context, "5001");
			context = SM2Utils.decrypt(context, "3165");
			System.out.println("明文：" + context);
			String orderId = "orderId";
			String tmp = context.replaceAll("\\\\", "");
			System.out.println(tmp);
			int index = tmp.indexOf(orderId);
			String tmp2 = tmp.substring(index + orderId.length());
			String[] tokens = tmp2.split("\"");
			System.out.println(tokens[2]);
			context = "true" + tokens[2];
		} catch (Exception e) {
			e.printStackTrace();
		}
//		DecryptAndVerifyService authService = new DecryptAndVerifyService();
//		authService.decrypyAndVerify(context);
		/*String fullPath = request.getScheme()+"://" + request.getServerName() +":"+ request.getServerPort() + base;
		logger.info("访问： "+fullPath);
		modelMap.addAttribute("base", base);*/
		
		try {
			PrintWriter pw = response.getWriter();
			pw.print(context);
			pw.flush();
			pw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
}
